﻿using AutomaticPush.Common;
using AutomaticPush.Extension;
using AutomaticPush.Quartz;
using Microsoft.Extensions.DependencyInjection;

namespace AutomaticPush
{
    public partial class PushFrom : Form
    {
        public PushFrom()
        {
            InitializeComponent();
        }
        private string NewLine = Environment.NewLine;
        private void PushBtn_Click(object sender, EventArgs e)
        {
            if (GlobalData.PushState)
            {
                GlobalData.PushState = false;
                PushBtn.Text = "开始推送";
                _ = Program.Provider.GetRequiredService<QuartzHostService>().StopAsync(new CancellationToken());
            }
            else
            {
                GlobalData.PushState = true;
                PushBtn.Text = "停止推送";
                _ = Program.Provider.GetRequiredService<QuartzHostService>().StartAsync(new CancellationToken());
            }

        }


        /// <summary>
        /// 用于其他线程信息推送
        /// </summary>
        /// <param name="text"></param>
        public void SetMessageTextBox(string text)
        {
            if (MessageTextBox.InvokeRequired)
            {
                //Action<string> action = new Action<string>(SetMessageTextBox);
                //Invoke(action, new object[] { text });
                Invoke((string text) => SetMessageTextBox(text));
            }
            else
            {
                var textVal = MessageTextBox.Text;
                if (!textVal.IsNullOrEmpty())
                    textVal = MessageTextBox.Text + NewLine;
                MessageTextBox.Text = $"{textVal}{text}";
            }
        }
    }
}
